Enable PrometheusMeterRegistry throwExceptionOnRegistrationFailure#671
Merged
Enable PrometheusMeterRegistry throwExceptionOnRegistrationFailure#671
Conversation
ar
approved these changes
Jan 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR The main change in this PR is in the
Q2class, enabling failure when thePrometheusMeterRegistrywon't register aMeterinstead of Micrometer silently accepting it. (there are a couple of extra minor things in other classes)Problem
Micrometer, allows the registration of multiple meters under the same name but with a different set of Tag keys.
(here I'm considering just the set of tag keys, not their values).
That means, I can register two meters, such as
Counters with namejpos.bbb.statsand{xxx="123", yyy="456"}{xxx="123", yyy="456", zzz="789"}Micrometer will happily accept that, and register both under its own registry, and it will return both valid and functioning
Meterinstances, that you can increment, and query their current count, etc. from the Java code.You can query the Micrometer registry and find both counters happily living there.
However, the underlying Prometheus registry will silently reject the second one because meters with the same name must have the exact same set of tag keys.
I say "silently", when in fact you will just get a stderr warning, that is very easy to miss, such as
So, your second meter wont be available for scraping.
The problem is that there is no easy way, programmatically, know that your meter failed to register under Prometheus.
By default, there is no boolean false, no null, not even an exception. There is no way to ask the returned
Meter"have you been successfully registered?" Furthermore, when querying thePrometheusMeterRegistryitself, the failedMeterappears in the list.All other solutions found, after long research, involve very expensive querying and streaming of obscure lists of internal objects.
Solution
The quick fix is to enable
throwExceptionOnRegistrationFailure()on thePrometheusMeterRegistry.So now, when you attempt registering an conflicting
Meter, three good things happen:IllegalStateException(This is an unchecked exception, so it could be catched or not.)MeterXYZ.builder(...etc...).register(reg)returnsnull, and you can check the return value if you don't want to catch the exception, and act accordingly.Meterwill not be registered. It will not appear when you doSearch.in(reg)or other similar queries.